{NEXT, PREV - arrays of longint; COUNT - number of elements}

procedure insert(n,k:longint); {inserts element N after K}
begin
prev[n]:=k;
next[n]:=next[k];
if next[k]<>0 then prev[next[k]]:=n;
next[k]:=n;
inc(count);
end;

procedure delete(n:longint); {deletes element N}
begin
if next[n]<>0 then prev[next[n]]:=prev[n];
if prev[n]<>0 then next[prev[n]]:=next[n];
dec(count);
end;